var constant = require('../../core/common/constant');
dishController.$inject = ['$scope', 'dishService'];

function dishController($scope, dishService) {
    dishService.getDishes(function (response) {
        if (response.data.result) {
            var dishes = response.data.data;

            for (var index = 0; index < dishes.length; index++) {
                // dishes[i].index = i;
                dishes[index].img = constant.FileServer + dishes[index].img + "?random=" + new Date().getTime();
            }

            $scope.dishes = dishes;
        } else {
            console.log(response.data.errMsg);
        }
    });

    $scope.toAdd=function () {
        document.getElementById('addName').value = '';
        document.getElementById('addType').value = '';
        document.getElementById('addPrice').value = '';
        document.getElementById('addImg').value = '';
        $("#toAdd").modal('show');
    };

    $scope.addDish = function () {
        var name = $scope.dishName;
        var type = $scope.dishType;
        var price = $scope.price;
        var dishImg = $scope.dishImg;
        var dish = {
            name: name,
            type: type,
            price: price,
            dishImg: dishImg
        };
        dishService.addDish(dish, function (response) {
            if (!response.data.result) {
                $('#toAdd').modal('hide');
                alert('add dish failed!');
            } else {
                $("#toAdd input").attr("value", '');
                $('#toAdd').modal('hide');
                alert('add dish success!');

                var dish = response.data.data;
                dish.img = constant.FileServer + dish.img + "?random=" + new Date().getTime();
                $scope.dishes.push(dish);
            }
        });
    };

    $scope.toUpdate = function (target) {
        var data = target.dataset.data;
        data = JSON.parse(data);
        document.getElementById('updateDid').value = data._id;
        document.getElementById('updateMid').value = data.mid;
        document.getElementById('updateName').value = data.name;
        document.getElementById('updateType').value = data.type;
        document.getElementById('updatePrice').value = data.price;
        $("#toUpdate").modal('show');
    };

    $scope.updateDish = function () {
        var mid = $("#toUpdate input[name='mid']").val();
        var did = $("#toUpdate input[name='did']").val();
        var name = $("#toUpdate input[name='dishName']").val();
        var type = $("#toUpdate select[name='dishType']").val();
        var price = $("#toUpdate input[name='price']").val();
        var updateImg = $scope.updateImg;
        var dish = {
            did: did,
            mid: mid,
            name: name,
            type: type,
            price: price,
            dishImg: updateImg
        };
        dishService.updateDish(dish, function (response) {
            if (!response.data.result) {
                $('#toUpdate').modal('hide');
                alert('update dish failed!');
            } else {
                $('#toUpdate').modal('hide');
                alert('update dish success!');
                dishService.getDishes(function (response) {
                    if (response.data.result) {
                        var dishes = response.data.data;
                        for (var i = 0; i < dishes.length; i++) {
                            dishes[i].index = i;
                            dishes[i].img = constant.FileServer + dishes[i].img + "?random=" + new Date().getTime();
                        }
                        $scope.dishes = dishes;
                    } else {
                        console.log(response.data.errMsg);
                    }
                });
            }
        });
    };

    $scope.toDelete = function (target) {
        var currentElement = target;
        var data = currentElement.dataset.data;
        data = JSON.parse(data);
        document.getElementById('delInput').value = data._id;
        $("#delete").modal('show');
    };

    $scope.deleteDish = function () {
        var did = $("#delete input[name='did']").val();

        dishService.deleteDish(did, function (response) {
            if (!response.data.result) {
                $('#delete').modal('hide');
                alert('delete dish failed!');
            } else {
                $('#delete').modal('hide');
                // alert('delete dish success!');
                dishService.getDishes(function (response) {
                    if (response.data.result) {
                        var dishes = response.data.data;
                        for (var i = 0; i < dishes.length; i++) {
                            dishes[i].index = i;
                            dishes[i].img = constant.FileServer + dishes[i].img + "?random=" + new Date().getTime();
                        }
                        $scope.dishes = dishes;
                    } else {
                        console.log(response.data.errMsg);
                    }
                });
            }
        });
    };

    $scope.searchDish = function () {
        var dtype = $("#dtype").val();
        var dname = $("#dname").val();
        if ((dname == '') || (dname == null) || (dname == 'undefined')) {
            dishService.findByType(dtype, function (response) {
                if (!response.data.result) {
                    alert('can not find by this condition');
                } else {
                    var dishes = response.data.data;
                    for (var i = 0; i < dishes.length; i++) {
                        dishes[i].index = i;
                        dishes[i].img = constant.FileServer + dishes[i].img + "?random=" + new Date().getTime();
                    }
                    $scope.dishes = dishes;
                }
            });
        } else {
            dishService.findByBoth(dtype, dname, function (response) {
                if (!response.data.result) {
                    alert('can not find by this condition');
                } else {
                    var dishes = response.data.data;
                    for (var i = 0; i < dishes.length; i++) {
                        dishes[i].index = i;
                        dishes[i].img = constant.FileServer + dishes[i].img + "?random=" + new Date().getTime();
                    }
                    $scope.dishes = dishes;
                }
            });
        }
    }
}

module.exports = dishController;